package com.xinghai.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinghai.admin.domain.SysLogininfor;
import com.xinghai.admin.domain.SysNotice;
import com.xinghai.admin.domain.SysNoticeRead;
import com.xinghai.admin.domain.SysOperLog;
import com.xinghai.admin.service.*;
import com.xinghai.common.constant.Constants;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.domain.entity.SysMenu;
import com.xinghai.common.core.domain.entity.SysUser;
import com.xinghai.common.core.domain.model.LoginBody;
import com.xinghai.common.core.domain.model.LoginUser;
import com.xinghai.common.utils.PageUtils;
import com.xinghai.common.utils.SecurityUtils;
import com.xinghai.security.service.SysLoginService;
import com.xinghai.security.service.SysPermissionService;
import com.xinghai.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 登录验证
 *
 * @author xinghai
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysNoticeReadService noticeReadService;

    @Autowired
    private ISysNoticeService noticeService;

    @Autowired
    private ISysOperLogService operLogService;

    @Autowired
    private ISysLogininforService logininforService;

    /**
     * 登录方法
     *
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        if (!loginUser.getPermissions().equals(permissions))
        {
            loginUser.setPermissions(permissions);
            tokenService.refreshToken(loginUser);
        }
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取通知公告列表
     *
     * @return 用户信息
     */
    @GetMapping("getNotice")
    public AjaxResult getNotice(String type,Integer pageNum,Integer pageSize)
    {
        if (pageNum==null)
            pageNum = 1;
        if (pageSize==null)
            pageSize = 10;
        PageUtils.startPage(pageNum,pageSize);
        PageUtils.orderBy("create_time desc");
        if ("1".equals(type)){
            SysNotice notice = new SysNotice();
            notice.setStatus("0");
            notice.setNoticeType("1");
            List<SysNotice> list = noticeService.selectNoticeList(notice);
            return AjaxResult.success(list);
        }else {
            LoginUser loginUser = SecurityUtils.getLoginUser();
            SysUser user = loginUser.getUser();
            SysNoticeRead notice = new SysNoticeRead();
            notice.setUserId(user.getUserId());
            List<SysNoticeRead> list = noticeReadService.list(notice);
            return AjaxResult.success(list);
        }
    }

    /**
     * 获取通知公告列表
     *
     * @return 用户信息
     */
    @GetMapping("readNotice")
    public AjaxResult readNotice(String noticeId)
    {
        if (noticeId==null){
            return AjaxResult.error("参数错误");
        }
        Long dataId = null;
        try {
            dataId = Long.valueOf(noticeId);
        }catch (Exception e){
            return AjaxResult.error("参数错误");
        }
        SysNotice sysNotice = noticeService.selectNoticeById(dataId,true);
        return AjaxResult.success(sysNotice);

    }


    /**
     * 获取通知公告列表
     *
     * @return 用户信息
     */
    @GetMapping("getLoginLog")
    public AjaxResult getLoginLog(Integer pageNum,Integer pageSize)
    {
        if (pageNum==null)
            pageNum = 1;
        if (pageSize==null)
            pageSize = 10;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        SysLogininfor sysLogininfor = new SysLogininfor();
        sysLogininfor.setUserName(user.getUserName());
        PageUtils.startPage(pageNum,pageSize);
        List<SysLogininfor> sysLogininfors = logininforService.selectLogininforList(sysLogininfor);
        return AjaxResult.success(sysLogininfors);
    }



    /**
     * 获取通知公告列表
     *
     * @return 用户信息
     */
    @GetMapping("getOperateLog")
    public AjaxResult getOperateLog(Integer pageNum,Integer pageSize)
    {
        if (pageNum==null)
            pageNum = 1;
        if (pageSize==null)
            pageSize = 10;
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setOperName(user.getUserName());
        PageUtils.startPage(pageNum,pageSize);
        List<SysOperLog> sysLogininfors = operLogService.selectOperLogList(sysOperLog);
        return AjaxResult.success(sysLogininfors);
    }
    /**
     * 获取路由信息
     *
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * SSO单点登录/无密码登录
     * @param username
     * @return
     */
    @PostMapping("/loginSso")
    public AjaxResult loginSso(@RequestParam String username) {
        AjaxResult ajax = new AjaxResult();
        String newToken = loginService.loginSso(username);
        ajax.put(Constants.TOKEN, newToken);
        return ajax;
    }
}
